using B.S.XZYData.Api.Read.Application.Command.Product;
using B.S.XZYData.Api.Read.Dto.Product;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Read.Controllers
{
    /// <summary>
    /// 产品读操作控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ProductController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ProductController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取所有产品列表（无条件显示）
        /// </summary>
        /// <returns>所有产品数据</returns>
        [HttpGet]
        public async Task<ApiResult<List<ProductDto>>> GetAllProducts()
        {
            var command = new GetAllProductsCommand();
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 获取产品分页列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="code">产品编号（模糊查询）</param>
        /// <param name="name">产品名称（模糊查询）</param>
        /// <param name="spec">规格（模糊查询）</param>
        /// <param name="unit">单位</param>
        /// <param name="producer">生产人员</param>
        /// <returns>产品分页数据</returns>
        [HttpGet]
        public async Task<ApiPageing<ProductDto>> GetProductPage(
            [FromQuery] int index = 1,
            [FromQuery] int size = 20,
            [FromQuery] string? code = null,
            [FromQuery] string? name = null,
            [FromQuery] string? spec = null,
            [FromQuery] string? unit = null,
            [FromQuery] string? producer = null)
        {
            var command = new GetProductPageCommand
            {
                Index = index,
                Size = size,
                Code = code,
                Name = name,
                Spec = spec,
                Unit = unit,
                Producer = producer
            };

            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据ID获取产品详情
        /// </summary>
        /// <param name="id">产品ID</param>
        /// <returns>产品详情</returns>
        [HttpGet]
        public async Task<ApiResult<ProductDto>> GetProductById(int id)
        {
            var command = new GetProductByIdCommand
            {
                Id = id
            };

            return await _mediator.Send(command);
        }
    }
} 